<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Everyday Series
Description: A three-column, fixed-width blog design.
Version    : 1.0
Released   : 20090617

-->
<?php 
	require_once('functions.php');	
	require_once('addLeaveForm.php');	
	session_start();
	
	$flag = "";
	$errorFlag = 0;
	$addLeaveFlag = 0;
	if(isset($_POST['submitAddLeave'])){
		$link = makeSQLconnection();
		$employeeNumber = $_POST['employeeNumber'];
		$result = checkEmpNo($employeeNumber);
		if(!$result){
			$errorFlag = 1;
		}else{
			$query = sprintf("SELECT * FROM `employee` WHERE 
							  `Employee_number` = '$employeeNumber'");
			$return = mysql_query($query, $link);
			
			if (!$return) {
				$success = 3;
			} else {
				$success = 1;
			}
			
			$found = false;
			if($success == 1) {
				while($data = mysql_fetch_assoc($return)){
					if($data['Employee_number'] == $employeeNumber) {
						$gender = $data['Employee_gender'];
						$found = true;
						if(isset($_POST['submitAddLeave']))
						$addLeaveFlag = 1;
						addLeaveForm :: addNewLeave ($employeeNumber, $gender);
					}
				}
			}
			
			if($found == false)
				$errorFlag=2;
			
			mysql_close($link);
		}
	}
	
	//SEARCH FOR NAME
	$checkFirstName = true;
	$checkMidName = true;
	$checkLastName = true;
	
	if(isset($_POST['searchForName'])){
		$link = makeSQLconnection();
		$empNo = "";
		$moreThanOneFlag = false;
		$failed = false;
		
		$firstName = $_POST['empFirstName'];
		$midName = $_POST['empMidName'];
		$lastName = $_POST['empLastName'];
		
		$checkFirstName = nameValidator($firstName);
		$checkMidName = nameValidator($midName);
		$checkLastName = nameValidator($lastName);
	
		if($checkFirstName && $checkMidName && $checkLastName){
			$errorFlag2 = false;
			$return = LeaveManager :: getLeaveDataName($_POST['empFirstName'], $_POST['empMidName'], $_POST['empLastName']);
			$employeeNumber = "";
			
			if (!$return) {
				$failed = true;
			} else {
				if (mysql_num_rows($return) == 1) {
					$data = mysql_fetch_assoc($return);
					$employeeNumber = $data['Employee_number'];
					$gender = $data['Employee_gender'];
				}else if (mysql_num_rows($return) > 1){
					$moreThanOneFlag = true;
				}else{
					$errorFlag = 2;
				}
			}
			
			if ($moreThanOneFlag) {
				echo "More than one employee found. Try to search by employee number, instead.";
			}else if($employeeNumber != ""){
				addLeaveForm :: addNewLeave ($employeeNumber, $gender);
				$addLeaveFlag = 1;
			}
			mysql_close($link);
		}
	}
	
	if ($addLeaveFlag != 1){
?>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
			<link href="template.css" rel="stylesheet" type="text/css" media="screen" />
			<title>Leave App Menu</title>
			<script src='jquery.js'></script>
			<script>
			$(document).ready(function(){
				
				$("a").css({
				'text-decoration':'none',
			   });
			   $("a").hover(function () {
					$(this).css({'text-decoration':'none','color':'blue','font-weight':'bold'});
				  }, function () {
					var cssObj = {
					  'text-decoration':'none',
					  'color' : '',
					  'font-weight':''
					}
					$(this).css(cssObj);
				  });
			   
			   $("#welcome").css({
				'font-size':'30px',
				'border':'5px solid white'
			   });
				
				$("#submitAddLeave").css({
					'background-color':'white',
					'color':'#2F2825'
				});
				$("#submitAddLeave").hover(function () {
					$(this).css({'background-color' : '#993333','color':'white','font-weight':'bold'});
				  }, function () {
					var cssObj = {
					  'background-color' : 'white',
					  'color' : '#2F2825',
					  'font-weight':''
					}
					$(this).css(cssObj);
				  });				
				  
				 $("#searchForName").css({
					'background-color':'white',
					'color':'#2F2825'
				});
				$("#searchForName").hover(function () {
					$(this).css({'background-color' : '#993333','color':'white','font-weight':'bold'});
				  }, function () {
					var cssObj = {
					  'background-color' : 'white',
					  'color' : '#2F2825',
					  'font-weight':''
					}
					$(this).css(cssObj);
				  }); 
			 });
			</script>
		</head>
<body>
<!-- start header -->
<div id="header">
	<div id="logo">
		<h1><span>UPLB</span> &nbsp; Leave Application</h1>
		<p></p>
	</div>
</div>
<!-- end header -->
<div id="wrapper">
	<!-- start page -->
	<div id="page">
	<div id="page-bg">
		<div id="sidebar1" class="sidebar">			
		</div>
		<!-- start content -->
		<div id="content">
			<div class="post">
				<div class="entry">
	<?php		
		if (isset($_SESSION['username'])) {
			$username = $_SESSION['username'];
	?>
						<div class='greeting' align="center"><p>Welcome, <?php echo $username; ?>!</p></div>			
						<table>
						<form name='addLeave' method='post' action='searchForAdd.php'>
							
							<tr>
							<td>Employee Number: </td>
							<td><input type='text' name='employeeNumber' id='employeeNumber'
							<?php 
								if (isset($_POST['employeeNumber'])) {
									echo "value='".$_POST['employeeNumber']."'/>";
								} else {
									echo "/></td>";
								}
							?>
							<?php
							if($errorFlag == 1){
								echo "<td><span class='error'>Invalid (ex: 123456789)</span></td>";
							}else if($errorFlag == 2){
								echo "<td><span class='error'>Employee not found</span></td>";
							}?>
							</br><br/>
							</tr>
							<tr>
							<td><input type='submit' class='buttons' name='submitAddLeave' id='submitAddLeave' value='Search'/></td>
							</tr>							
						</form>
							<tr>
							<td>&nbsp;</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
							</tr>							
						<form name='searchLeaveName' method='post' action='searchForAdd.php'>
							<tr>
							<td>First Name: </td>
							<td><input type='text' name='empFirstName'
						<?php
						echo "value='";
						if (isset($_POST['empFirstName'])) {
							echo $_POST['empFirstName'];
						} else echo"";
						echo "'/></td>";									
						?>
							<td>
						<?php
						if (!$checkFirstName) echo "<span class='error'>Invalid (ex: Rommel)</span>";
						?>
							</td>
							</tr>
							<tr>
							<td>Middle Name: </td>
							<td><input type='text' name='empMidName' 
						<?php
						echo "value='";
						if (isset($_POST['empMidName'])) {
							echo $_POST['empMidName'];
						} else echo"";
						echo"'/></td>";
						?>
							<td>
						<?php
						if (!$checkMidName) echo "<span class='error'>Invalid (ex: Tralala)</span>";
						?>
							</td>
							</tr>
							<td>Last Name: </td>
							<td><input type='text' name='empLastName' 
						<?php
						echo "value='";
						if (isset($_POST['empLastName'])) {
							echo $_POST['empLastName'];
						} else echo"";
						echo"'/></td>";						
						?>
							<td>
						<?php
						if (!$checkLastName) echo "<span class='error'>Invalid (ex: Bulalacao)</span>";
						?>
							</td>
							</tr>
							<tr>
							<td><input type='submit' class='buttons' name='searchForName' id='searchForName' value='Search' /></td>
							</tr>
						</form>
						</table>
						<p class="links"><a href="menu.php" class="more">&laquo;&laquo;&nbsp;&nbsp;Home&nbsp;&nbsp;&raquo;&raquo;</a></p>					

	<?php } else {
	?>
			You're not supposed to be here!<br/><br/>
			Would you like to <a href='login.php'>Log In</a> or <a href='register.php'>Register</a> ? <br/>
	<?php	
		}
	?>	
				</div>
			</div>
		</div>
		<!-- end content -->
		<!-- start sidebars -->
		<div id="sidebar2" class="sidebar">
		</div>
		<!-- end sidebars -->
		<div style="clear: both;">&nbsp;</div>
	</div>
	</div>
	<!-- end page -->
</div>
<div id="footer">	
</div>
</body>
</html>

<?php
	}
?>